<template>
  <a-spin :spinning="loading">
    <div>
      <a-form-model
        class="fromModels"
        :model="config"
        ref="courseCourtshipModel"
        layout="vertical"
        :labelCol="{ span: 4 }"
        :wrapperCol="{ span: 12, offset: 1 }"
      >
        <a-form-model-item label="提现返佣开关">
          <a-radio-group v-model="status">
            <a-radio :value="1"> 开 </a-radio>
            <a-radio :value="0"> 关 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="税费费率">
          <a-input v-model="config.tax_rate">
            <div slot="addonAfter">万</div>
          </a-input>
        </a-form-model-item>
        <a-form-model-item
          v-for="(item, index) in config.rate"
          :key="'rate' + index"
          :prop="'rate.' + index"
          label="级别-比例"
        >
          <div class="disse">
            <a-select :disabled="disableds" v-model="item.id">
              <a-select-option v-for="(item, index) in levelArrs" :key="index" :value="item.level">{{
                item.name
              }}</a-select-option>
            </a-select>
            <span class="margin_lr5">-</span>
            <a-input v-model="item.rate">
              <div slot="addonAfter">%</div>
            </a-input>
          </div>
        </a-form-model-item>
        <a-form-model-item
          v-for="(item, index) in config.team"
          :key="'team' + index"
          :label="index + '级返佣比例'"
          :rules="[{ required: true, message: '请输入' + index + '级返佣比例', trigger: 'blur' }]"
        >
          <a-input v-model="config.team[index]" placeholder="请输入">
            <div slot="addonAfter">%</div>
          </a-input>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 10 }">
          <a-button type="primary" @click="lineChanges('team', 'add')">新增一行</a-button>
          <a-button type="primary" @click="lineChanges('team', 'del')">删除最后一行</a-button>
        </a-form-model-item>

        <a-form-model-item :wrapper-col="{ span: 12, offset: 5 }">
          <a-button type="primary" @click="whites">提交</a-button>
          <a-button type="primary" @click="reset">重置</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-spin>
</template>

<script>
import { getRebateApi, setRebateApi, getLevelApi } from '@/api/return.js'
export default {
  name: 'courseCourtship',

  data() {
    return {
      loading: false,
      config: {
        rate: [],
        team: [],
        tax_rate: '0'
      },
      status: 1,
      levelArrs: [],
      disableds: true
    }
  },
  created() {
    this.getPrivacyAgreementInfo()
    this.getLevelApi()
  },
  methods: {
    reset() {
      this.getPrivacyAgreementInfo()
      this.getLevelApi()
    },
    lineChanges(key, type) {
      if (type == 'add') {
        this.config[key].push('')
      }
      if (type == 'del') {
        this.config[key].splice(this.config[key].length - 1, 1)
      }
    },
    getLevelApi() {
      getLevelApi({
        brand_id: process.env.VUE_APP_BRAND_ID
      }).then(res => {
        this.levelArrs = res.data
      })
    },
    getPrivacyAgreementInfo() {
      getRebateApi({
        type: 'course_rebate',
        brand_id: process.env.VUE_APP_BRAND_ID
      }).then(res => {
        this.config = res.data.config
        this.status = res.data.status
      })
    },
    whites() {
      this.setDatas()
    },
    setDatas() {
      setRebateApi({
        type: 'course_rebate',
        status: this.status,
        brand_id: process.env.VUE_APP_BRAND_ID,
        data: this.config
      }).then(res => {
        this.getPrivacyAgreementInfo()
        this.getLevelApi()
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
